package logic

import (
	"context"
	"mini/minirpc/minirpc"
	"mini/pkg"

	"mini/miniapi/internal/svc"
	"mini/miniapi/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ProductAddLogic) ProductAdd(req *types.ProductAddRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	add, err := l.svcCtx.MiniRpc.ProductAdd(l.ctx, &minirpc.ProductAddRequest{
		Image:  req.Image,
		Video:  req.Video,
		Name:   req.Name,
		Num:    req.Num,
		WhyBuy: req.WhyBuy,
		UserId: int64(req.UserId),
	})
	if err != nil {
		return &types.Response{
			Message: pkg.String(err.Error()),
			Code:    500,
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Message: "商品添加成功",
		Code:    200,
		Data:    add,
	}, nil
}
